package com.longrise.co.ui.mine.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.co.R;
import com.longrise.co.ui.mine.contract.MineContract;
import com.longrise.co.ui.mine.model.EvaluationDetailBean;
import com.longrise.co.ui.mine.model.MineModel;
import com.longrise.co.ui.mine.presenter.MinePresenter;
import com.ly.common.base.BaseFragment;
import com.ly.common.commonutils.StringUtils;
import com.ly.common.commonutils.TimeUtil;
import com.ly.common.dialog.DialogOnClickListener;
import com.ly.common.dialog.MDAlertDialog;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by luoyang on 2017-7-21.
 */

public class EvaluationDetailFragment extends BaseFragment<MinePresenter, MineModel> implements MineContract.View<EvaluationDetailBean>,DialogOnClickListener {


    @Bind(R.id.head_title)
    TextView headTitle;
    @Bind(R.id.complaint_name)
    TextView complaintName;
    @Bind(R.id.ratingBar)
    RatingBar ratingBar;
    @Bind(R.id.complaint_location_tv)
    TextView complaintLocationTv;
    @Bind(R.id.complaint_call_tv)
    TextView complaintCallTv;
    @Bind(R.id.car_no_value)
    TextView carNoValue;
    @Bind(R.id.detail_date)
    TextView detailDate;
    @Bind(R.id.detail_desc)
    TextView detailDesc;
    @Bind(R.id.detail_price)
    TextView detailPrice;
    @Bind(R.id.service_attitude_ratingBar)
    RatingBar serviceAttitudeRatingBar;
    @Bind(R.id.repair_effectiveness_ratingBar)
    RatingBar repairEffectivenessRatingBar;
    @Bind(R.id.repair_price_ratingBar)
    RatingBar repairPriceRatingBar;
    @Bind(R.id.storefront_environment_ratingBar)
    RatingBar storefrontEnvironmentRatingBar;
    @Bind(R.id.arrow_right)
    ImageView arrowRight;
    @Bind(R.id.evaluation_detail_value)
    TextView evaluationDetailValue;
    @Bind(R.id.evaluation_detail_value_rl)
    RelativeLayout evaluationDetailValueRl;

    @Bind(R.id.replay_arrow_right)
    ImageView replyArrowRight;
    @Bind(R.id.company_reply_value)
    TextView companyReplyValue;
    //企业回复内容
    @Bind(R.id.company_reply_rl)
    RelativeLayout companyReplyRl;
    //企业回复
    @Bind(R.id.company_reply)
    RelativeLayout companyReply;

    private String mEvaluationId;
    private String mAreaId;

    private boolean isEx = true;
    private MDAlertDialog mCallDialog;
    private EvaluationDetailBean mData;

    @Override
    protected int getLayoutResource() {
        return R.layout.fg_evaluation_detail;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        mEvaluationId = getArguments().getString("evaluationId");
        mAreaId = getArguments().getString("areaId");
        setText(headTitle, "评价详情");
        mPresenter.getEvaluationDetail(mEvaluationId, mAreaId);
    }

    public static EvaluationDetailFragment newInstance(String evaluationId, String areaId) {

        Bundle args = new Bundle();
        args.putString("evaluationId", evaluationId);
        args.putString("areaId", areaId);
        EvaluationDetailFragment fragment = new EvaluationDetailFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void requestSuccess(EvaluationDetailBean bean) {
        if(bean == null) {
            return;
        }
        try {
            mData = bean;
            setText(complaintName, bean.getCompanyname());

//        int star = new BigDecimal(bean.getStar()).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
            if (!StringUtils.isEmpty(bean.getStar())) {
                ratingBar.setRating(Float.parseFloat(bean.getStar()));
            }

            setText(complaintLocationTv, bean.getCompanyaddress());
            setText(complaintCallTv, bean.getBusphone());
            setText(carNoValue, bean.getCarno());
            setText(detailDate, TimeUtil.getDateYMD(bean.getRepairdate()));
            setText(detailDesc, bean.getFaultdescription());
            setText(detailPrice, bean.getEvaluatetotle());

            if(!StringUtils.isEmpty(bean.getEvaluateservice())) {
                serviceAttitudeRatingBar.setRating(Float.parseFloat(bean.getEvaluateservice()));
            }
            if(!StringUtils.isEmpty(bean.getEvaluateefficiency())) {
                repairEffectivenessRatingBar.setRating(Float.parseFloat(bean.getEvaluateefficiency()));
            }

            if(!StringUtils.isEmpty(bean.getEvaluateprice())) {
                repairPriceRatingBar.setRating(Float.parseFloat(bean.getEvaluateprice()));
            }

            if(!StringUtils.isEmpty(bean.getEvaluateenvironment())) {
                storefrontEnvironmentRatingBar.setRating(Float.parseFloat(bean.getEvaluateenvironment()));
            }

            setText(evaluationDetailValue, bean.getEvaluatetotledetails());

            isEx = true;
            setEvaluationDetail();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setEvaluationDetail() {
        evaluationDetailValueRl.setVisibility(isEx ? View.VISIBLE : View.GONE);
        arrowRight.setImageResource(isEx ? R.drawable.top : R.drawable.arrow_right);
    }

    private void initCallDialog() {
        mCallDialog = new MDAlertDialog.Builder(getContext())
                .setContentText("确定拨打该企业电话？")
                .setOnclickListener(this)
                .build();
    }

    @Override
    public void onRequestComplete() {

    }

    @OnClick({R.id.head_back, R.id.evaluation_detail, R.id.complaint_call_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head_back:
                _mActivity.onBackPressed();
                break;
            case R.id.evaluation_detail:
                isEx = !isEx;
                setEvaluationDetail();
                break;
            case R.id.complaint_call_iv:
                if(mCallDialog == null) {
                    initCallDialog();
                }
                mCallDialog.show();
                break;
        }
    }

    @Override
    public void clickLeftButton(View view) {
        if(mCallDialog != null) {
            mCallDialog.dismiss();
        }
    }

    @Override
    public void clickRightButton(View view) {
        //拨打电话
        if(mData != null && !StringUtils.isEmpty(mData.getBusphone())) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + mData.getBusphone()));
            startActivity(intent);
        }

        if(mCallDialog != null) {
            mCallDialog.dismiss();
        }
    }
}
